package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.car.BargainRecord;
import com.gongpingjialoan.utility.TimeUtils;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by Administrator on 2016/5/19.
 */
public class BargainRecordAdapter extends BaseAdapter {

    private List<BargainRecord> mBargainRecord;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    public BargainRecordAdapter(List<BargainRecord> assessHistories, Context context) {
        mBargainRecord = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<BargainRecord> mBargainRecord) {
        this.mBargainRecord = mBargainRecord;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mBargainRecord.size();
    }

    @Override
    public Object getItem(int position) {
        return mBargainRecord.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.bargain_record_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.time);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.mile);
            holder.priceTextView = (TextView) convertView.findViewById(R.id.price);
            holder.valueTextView = (TextView) convertView.findViewById(R.id.bargain_value);
            holder.bargainResult = (TextView) convertView.findViewById(R.id.bargain_result);
            holder.bargainSource = (TextView) convertView.findViewById(R.id.bargain_source);
            holder.hadsellTv = (TextView) convertView.findViewById(R.id.review);
            holder.evalView = convertView.findViewById(R.id.value_ll);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }
        BargainRecord source = mBargainRecord.get(position);
        convertView.setTag(R.id.tag_second, source);
        if (source != null) {
            if ("sale".equals(source.car_status)) {
                holder.hadsellTv.setVisibility(View.GONE);
            } else {
                holder.hadsellTv.setVisibility(View.VISIBLE);
            }
            if ("s".equals(source.status)) {
                holder.bargainResult.setVisibility(View.VISIBLE);
            } else {
                holder.bargainResult.setVisibility(View.GONE);
            }

            holder.tvCarTitle.setText(source.title);
            holder.mileTextView.setText(source.year + "年上牌/" + source.mile + "万公里");
            holder.timeTextView.setText(TimeUtils.timeFormat(source.pub_time));
            holder.priceTextView.setText(source.price);
            holder.valueTextView.setText(source.expected_price);
            if ("".equals(source.eval_price) || Utils.isLing(source.eval_price) ) {
                holder.evalView.setVisibility(View.GONE);
            } else {
                holder.bargainSource.setText(source.eval_price);
                holder.evalView.setVisibility(View.VISIBLE);
            }

            Glide.with(mContext).load(source.thumbnail).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);
        }
        return convertView;
    }


    public static class ViewHolder {
        public ImageView imgThumbnail;

        public TextView tvCarTitle;

        public TextView timeTextView;

        public TextView mileTextView;

        public TextView priceTextView;

        public TextView reviewTextView;

        public View evalView;

        public TextView valueTextView;

        public TextView bargainResult;

        public TextView bargainSource;

        public TextView hadsellTv;
    }
}

